//优惠券类型 1 无门槛 2 满减
var curCouponType = "";


var $table = $("#list_table");

$(function () {
    var url = urlcore + "/api/deliveryPlan/fetchDeliPlansAdmin";
    initTable($table, url);
});

//筛选优惠券类型，刷新列表
$("#curType").change(function () {
    curCouponType = $("#curType").val();
    $table.bootstrapTable("refresh");
});


var columns = [
    {
        title: "序号",
        align: "center",
        valign:"middle",
        width: "50px",
        formatter: function (value, row, index) {
            return (index + 1);
        }
    }, {
        field: "id",
        title: "方案id",
        halign: "center",
        align: "center",
        valign:"middle",
        width: "50px",
        // visible: false
    }, {
        field: "title",
        title: "方案详情",
        halign: "center",
        align: "center",
        width: "300px",
        formatter: function (value, row, index) {
            var html = "";
            html += "<li class=\"shipping-item\">\n" +
                "    <div class=\"shipping-title\" style='text-align: left'><b>"+row.title+"</b>\n" +
                "        <div class=\"pull-right\"><span class=\"clr-gray ng-binding\">创建时间"+row.gmtDatetime+"</span>&nbsp;&nbsp; <span\n" +
                "                class=\"delimiter\">|</span>" +
                " <a href=\"delivery_plan_add.html?id=" + row.id + "\"><i class=\"fa fa-edit\"></i>\n" +
                "            修改</a> <span class=\"delimiter\">|</span></div>\n" +
                "    </div>\n" +
                "    <table class=\"table\">\n" +
                "        <tbody>\n" +
                "        <tr>\n" +
                "            <td>配送区域</td>\n" +
                "            <td class=\"num-cell\">首件运费(元)</td>\n" +
                "            <td class=\"num-cell\">续件运费(元)</td>\n" +
                "        </tr>\n";
            var itemsStr = "";
            for (var item of row.deliveryItems) {
                var area = convertArea(item.areaNo);
                var firstPostage=item.firstPostage;
                var continuePostage=item.continuePostage;
                itemsStr +=
                    "        <tr>\n" +
                    "            <td><span>"+area+"</span></td>\n" +
                    "            <td class=\"num-cell\">"+firstPostage+"</td>\n" +
                    "            <td class=\"num-cell\">"+continuePostage+"</td>\n" +
                    "        </tr>\n";
                ;
            }
            html+=itemsStr;
            html += "        </tbody>\n" +
                "    </table>\n" +
                "</li>";
            return html;
        }
    },{
        field: "zz",
        title: "",
        halign: "center",
        align: "center",
        valign:"middle",
        width: "50px"
    },];

//查询参数
var queryParams = function (params) {
    var search = params.search == undefined ? "" : params.search;
    console.log("params:", params);
    var param = {
        offset: params.offset,
        current: params.offset / params.limit + 1,//bootstrap-table分页参数offset从0开始，计算的后端当前页参数current=(offset/limit+1)，
        size: params.limit,
        condition: {
            searchText: search
        }
    };

    return param;
};

//初始化表格，有已有和待选的商品两类表格 tableins:表格实例 ,
// insTag 0表示当前已添加表格 1表示待添加的表格
function initTable(tableins, url) {
    tableins.bootstrapTable({
        columns: columns,
        method: "post",
        url: url,
        dataType: "json",
        queryParams: function (params) {
            return queryParams(params);
        },
        //返回数据预处理
        responseHandler: function (cbdata) {
            console.log(cbdata);
            if (null != cbdata.data && null != cbdata.data.records) {
                var records = cbdata.data.records;
                var res = {
                    //如果采用server端分页，必须有rows和total这两部分数据
                    "rows": records,
                    "total": cbdata.data.total
                };
                return res;
            } else return {"rows": [], "total": 0};
        },
        cache: false,
        uniqueId: "id",
        // toolbar: '#toolbar',//自定义工具栏，不同表格工具栏不同，一个jQuery 选择器，指明自定义的toolbar 例如:#toolbar, .toolbar.
        pagination: true,//启用分页条
        sidePagination: "server",//分页方式client或server
        // pageNumber: 1,
        pageSize: 8,//每页大小
        pageList: [8, 16, 32, 48],//可以选择每页大小
        showRefresh: true,//显示刷新
        sortable: true,//是否启用排序
        // clickToSelect: true,//设置true 将在点击行时，自动选择rediobox 和 checkbox
        search: true,//搜索,未设置分页方式为"server"时只是客户端搜索
        // searchText:"",//初始搜索文本
    });
}

//刪除
function doDelete(id) {
    confirm("确认删除吗？", function (callback) {
        if (callback) {
            delCoupon();
        }
    });

    function delCoupon() {
        $.ajax({
            type: "GET",
            url: urlcore + "/api/message/delMsg?id=" + id,
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: {},
            success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
                if (data.success == true) {
                    $table.bootstrapTable("refresh");
                } else {
                    alert(data.msg);
                }
            },
            error: function () {
                alert("删除失败");
            }
        });
    }
}

//转化地区
function convertArea(areaNo) {
    var area;
    switch (areaNo) {
        case 1:
            area="江浙沪";
            break;
        case 2:
            area="北京、天津、江西、安徽、广东、福建、山东、河北、河南、湖北、湖南";
            break;
        case 3:
            area="四川、重庆、贵州、云南、广西、山西、陕西、海南、辽宁、黑龙江、吉林";
            break;
        case 4:
            area="甘肃、宁夏、青海、内蒙古、新疆、西藏";
            break;
    }
    return area;
}